<html>
    <head>
        <title>Profile Page</title>
        <link href="style3.css" rel="stylesheet" type="text/css" />
    </head>
    <body>         
        <form name= "form" action = "EditInfo.php" value = "Submit" method = "Post">

    </body>

    <?php
    //Author Nour Ossama
    //starting session
    session_start();
    //include fucntions.php
    include ("functions.php");
    //check if the variable is passed from the "My Profile" button
    if (isset($_GET['property_variable'])) {
        /* this is a member trying to access his own profile 
         *  take id from session and store in variable $member_id
         */
        $member_id = $_SESSION['member_id'];
        //calling to execute function if condition was satisfied
        ProfilePage($member_id);
    }
    //if variable was not passed
    else if (!isset($_GET['property_variable'])) {
        /* this is a member trying to access profiles of other members
         * so take member_id passed from previous page by get and store 
         * in variable $memberID
         */
        $memberID = intval($_GET['member_id']);
        //calling to execute function if condition was satisfied
        ProfilePage($memberID);
    } else {//if nothing was passed
        echo "Sorry, action cannot be performed!";
    }


    /*
     * function ProfilePage takes member_id and returns a table 
     * with all the info about the member
     */

    Function ProfilePage($member_id) {

        $conn = mysql_connect('localhost', 'root', ''); //connect to the database

        if (!$conn) {//if no connection was made
            die('Could not connect:' . mysql_error());
        }

        mysql_select_db("mydb", $conn); //select database
        /*
         * query check _member checks if the member entered any info other than the email and password if not,
         * a message will be displayed otherwise the profile page is viewed
         */
        $check_member = mysql_query("SELECT *
                                     FROM member
                                     WHERE member_id = '$member_id'") or die(mysql_error());
        while ($row = mysql_fetch_assoc($check_member)) {
            if ($row['first_name'] == NULL && $row['last_name'] == NULL && $row ['phoneNum'] == NULL
                    && $row['fax'] == NULL && $row['address'] == NULL && $row['affiliation'] == NULL
                    && $row ['fb_page'] == NULL && $row['linked_page'] == NULL && $row['website'] == NULL
                    && $row['research_gate_site'] == NULL && $row['academic_site'] == NULL && $row['mendeley_site'] == NULL) {
                //display a message to the member
                echo "<p>There is no information to be displayed yet!</p>";
            } else {

                if (!isset($_GET['member_id'])) {
                    //displaying a hyperlink at the top right corner that redirects to the EditInfo page
                    print "<a style=\"float:right\" href=\"EditInfo.php?member_id={$member_id}\">Edit Information</a></br>";
                }
                //using get_first_name function to display first name
                $first_name = get_first_name($member_id);
                //using get_last_name function to display last name
                $last_name = get_last_name($member_id);
                //displaying first and last names as the page header
                print "<h1>{$first_name} {$last_name}</h1>";
                //query that retrieves the member's first name
                $get_first_name = mysql_query("SELECT first_name 
                                               FROM member
                                               WHERE member_id = '$member_id'") or die(mysql_error());
                //open table
                echo '<table width="20%" id=11>
                <tr>
                <td><b>First Name:</b></td>';
                //fetch first name
                while ($row = mysql_fetch_assoc($get_first_name)) {
                    /*
                     * check if first name is null i.e no first name was entered 
                     * then display an empty cell
                     */
                    if ($row['first_name'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's first name in a cell
                        echo "<td>{$row['first_name']}</td>
                        </tr>";
                    }
                }
                echo '<tr>
                    <td><b>Last Name:</b></td>';
                //query that retrieves the member's last name
                $get_last_name = mysql_query("SELECT last_name
                                              FROM member
                                              WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_last_name)) {
                    /*
                     * check if last name is null i.e no last name was entered
                     * then display an empty cell
                     */

                    if ($row['last_name'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's last name in a cell
                        echo "<td>{$row['last_name']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Email:</b></td>";
                //query that retrieves the member's main email
                $get_main_email = mysql_query("SELECT email
                                               FROM member
                                               WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_main_email)) {
                    /*
                     * check if email is null i.e no email was entered
                     * then display an empty cell
                     */
                    if ($row['email'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's main email in a cell
                        echo "<td>{$row['email']}</td>
                        </tr>";
                    }
                }
                //query that retireves other emails of the member
                $get_other_email = mysql_query("SELECT email
                                                FROM emails
                                                WHERE member_id = '$member_id'") or die(mysql_error());
                //check if the result of the query is empty
                if (!mysql_num_rows($get_other_email)) {
                    echo "<tr>
                        <td><b>Other Email(s):</b></td>";
                    echo "<td>-</td>";
                    //check if there is other emails in the db, display in table cell
                } else if ($get_other_email) {
                    while ($row = mysql_fetch_assoc($get_other_email)) {
                        echo "<tr>
                        <td><b>Other Email(s):</b></td>
                        <td>{$row['email']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Phone Number:</b></td>";
                //query that retrieves member's phone number
                $get_phone_num = mysql_query("SELECT phoneNum
                                              FROM member
                                              WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_phone_num)) {
                    /*
                     * check if phone is null i.e no phone num  was entered
                     * display an empty cell
                     */
                    if ($row['phoneNum'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's phone num in a cell
                        echo "<td>{$row['phoneNum']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Fax Number:</b></td>";
                //query that retrieves member's fax number
                $get_fax = mysql_query("SELECT fax
                                        FROM member
                                        WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_fax)) {
                    /*
                     * check if fax is null i.e no phone num  was entered
                     * display an empty cell
                     */
                    if ($row['fax'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's phone num in a cell
                        echo "<td>{$row['fax']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Address:</b></td>";
                //query that retrieves member's address
                $get_address = mysql_query("SELECT address
                                            FROM member
                                            WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_address)) {
                    /*
                     * check if address is null i.e no address was entered
                     * display an empty cell
                     */
                    if ($row['address'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's address in a cell
                        echo "<td>{$row['address']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Affiliation:</b></td>";
                //query that retrieves member's address
                $get_affiliation = mysql_query("SELECT affiliation
                                                FROM member
                                                WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_affiliation)) {
                    if ($row['affiliation'] == NULL) {
                        /*
                         * check if affiliation is null i.e no affiliation was entered
                         * display an empty cell
                         */
                        echo "<td>-</td>";
                    } else {
                        //display the member's affiliation in a cell
                        echo "<td>{$row['affiliation']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Facebook Page:</b></td>";
                //query that retrieves member's facebook page
                $get_fbpage = mysql_query("SELECT fb_page
                                           FROM member
                                           WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_fbpage)) {
                    /*
                     * check if fb_page is null i.e no facebook page was entered
                     * display an empty cell
                     */
                    if ($row['fb_page'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's facebook page url in a cell
                        echo "<td>{$row['fb_page']}</td>
                        </tr>";
                    }
                }

                echo "<tr>
                    <td><b>LinkedIn Page:</b></td>";

                //query that retrieves member's LinkedIn page
                $get_linked_page = mysql_query("SELECT linked_page
                                                FROM member
                                                WHERE member_id = '$member_id'") or die(mysql_error());

                while ($row = mysql_fetch_assoc($get_linked_page)) {
                    /*
                     * check if linked_page is null i.e no LinkedIn page was entered
                     * display an empty cell
                     */
                    if ($row['linked_page'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's LinkedIn page url in a cell
                        echo "<td>{$row['linked_page']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Website:</b></td>";
                //query that retrieves member's website
                $get_website = mysql_query("SELECT website
                                            FROM member
                                            WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_website)) {
                    /*
                     * check if website is null i.e no website was entered
                     * display an empty cell
                     */
                    if ($row['website'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's website url in a cell
                        echo "<td>{$row['website']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Research Gate Site:</b></td>";
                //query that retrieves member's research gate site
                $get_research_gate_site = mysql_query("SELECT research_gate_site
                                                       FROM member
                                                       WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_research_gate_site)) {
                    /*
                     * check if research gate site is null i.e no research gate site was entered
                     * display an empty cell
                     */
                    if ($row['research_gate_site'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's research gate site url in a cell
                        echo "<td>{$row['research_gate_site']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Academic Site:</b></td>";
                //query that retrieves member's academic site
                $get_academic_site = mysql_query("SELECT academic_site
                                                  FROM member
                                                  WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_academic_site)) {
                    /*
                     * check if academic site is null i.e no academic site was entered
                     * display an empty cell
                     */
                    if ($row['academic_site'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's academic site url in a cell
                        echo "<td>{$row['academic_site']}</td>
                        </tr>";
                    }
                }
                echo "<tr>
                    <td><b>Mendeley Site:</b></td>";
                //query that retrieves member's mendeley site
                $get_mendeley_site = mysql_query("SELECT mendeley_site
                                                  FROM member
                                                  WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($get_mendeley_site)) {
                    /*
                     * check if mendeley site is null i.e no mendeley site was entered
                     * display an empty cell
                     */
                    if ($row['mendeley_site'] == NULL) {
                        echo "<td>-</td>";
                    } else {
                        //display the member's mendeley site url in a cell
                        echo "<td>{$row['mendeley_site']}</td>
                        </tr>";
                    }
                }
                //close table
                echo '</table>';
            }
        }
        //close connection
        mysql_close($conn);
    }
    ?>
</html>










